import axios from 'axios';
import Router from '@/router/index'
import de from "element-ui/src/locale/lang/de";
//import qs from 'qs';

let channelVal = "123456123"
// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
             "x-channelkey":channelVal
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res =>{
      localStorage.setItem('logintoken', res.headers['x-token'])
      localStorage.setItem('x-sessionId', res.headers['x-session-id'])
      return res.data
    } );
};
// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
             'x-token': localStorage.getItem('logintoken'),
             'x-session-id': localStorage.getItem('x-sessionId'),
            'x-channelkey':channelVal
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => {
      assertLoginOK(res)
      return res.data
    });
};

const reqNoParams = (method, url) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'x-token': localStorage.getItem('logintoken'),
      'x-session-id': localStorage.getItem('x-sessionId'),
      'x-channelkey':channelVal
    }
  }).then(res => {
    assertLoginOK(res)
    return res.data
  });
};

// 通用公用方法
const reqWithJSON = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json',
      'x-token': localStorage.getItem('logintoken'),
      'x-session-id': localStorage.getItem('x-sessionId'),
      'x-channelkey':channelVal
    },
    data: params,
    traditional: true,
    transformRequest: [
      function(data) {
        let jsonData =JSON.stringify(data);
        debugger
        return jsonData
      }
    ]
  }).then(res => {
    let f = assertLoginOK(res)
    if(f){
      return res.data
    }
  });
};

function assertLoginOK(resp){
   let code = resp.data.code;

  debugger
   if(code === '9997' || code ==="9992" || code === "9996"){
     localStorage.removeItem('logintoken')
     localStorage.removeItem('userdata')
     localStorage.removeItem('userInfo')
     localStorage.removeItem('x-sessionId')
     Router.push({path: '/login'})
     return false
   }
   return true
}

export {
    loginreq,
    req,reqWithJSON,reqNoParams
}
